﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE_TOOL.Tool.UserControls;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_TOOL.Tool.Forms
{
	public partial class frm_MistakeEdit : DevExpress.XtraEditors.XtraForm
	{
		public string MistakeId;
		public string MistakeName;
		public string MistakeNameJapan;
        private LogUtils log;
        private ucMistake parent;

		public frm_MistakeEdit(ucMistake fMistake)
		{
            InitializeComponent();
            btSave.Image = ERP_RORZE_TOOL.Properties.Resources.accept;
            btCancel.Image = ERP_RORZE_TOOL.Properties.Resources.cancel;
			parent = fMistake;
			this.CancelButton = this.btCancel;
            log = new LogUtils();
        }

		private void btSave_Click(object sender, EventArgs e)
		{
			MistakeHelper helper = new MistakeHelper(new Mistake());
			if(GetData(helper.ValueObject))
			{
				if(helper.Update() > 0)
				{

                    //write Log
                    log.Table = LogConstant.Table.ToolCutting.Mistake;
                    log.Task = LogConstant.Task.Update;
                    log.Description = helper.ValueObject.MistakeID;
                    log.WriteLog();

                    if (!Utilities.MessageConfirmationYesNo(ToolConstants.Message.UpdateSuccessAndQuestion))
                    {
                        this.Close();
                    }
                }
				else
				{
					
					Utilities.MessageErrorOk(ToolConstants.Message.UpdateFailed);
				}
				parent.BindDatatoGrid();
			}
		}

		private void btCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void frm_MistakeEdit_Load(object sender, EventArgs e)
		{
			BindData();
		}

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }
		#region Private methods

		private bool CheckDuplicate(string id)
		{
			MistakeHelper helper = new MistakeHelper(new Mistake());
			helper.ValueObject.MistakeID = id;
			DataTable dt = helper.GetOne();
			if(dt.Rows.Count > 0)
				return true;
			return false;

		}
		private bool GetData(Mistake o)
		{
			#region Check valid
			if(Utilities.IsNullOrEmpty(txtMistakeID.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Mistake.MistakeIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtMistakeName.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Mistake.MistakeNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(string.Compare(txtMistakeID.Text.Trim(), MistakeId) != 0)
			{
				if(CheckDuplicate(txtMistakeID.Text.Trim()))
				{
					MessageBox.Show(ToolConstants.Message.Mistake.MistakeHasExisted, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
					return false;
				}
			}

			#endregion

			o.TempId = txtMistakeID.Text;
			o.MistakeID = MistakeId;
			o.MistakeEnglish = txtMistakeName.Text;
			o.MistakeJanpanese = txtMistakeJapanese.Text;

			return true;
		}

		//private void BindData(Mistake o)
		//{
		//	txtMistakeID.Text = o.MistakeID;
		//	txtMistakeName.Text = o.Mistake_English;
		//	txtMistakeJapanese.Text = o.Mistake_Japanese;
		//}

		private void BindData()
		{
			txtMistakeID.Text =MistakeId;
			txtMistakeName.Text = MistakeName;
			txtMistakeJapanese.Text = MistakeNameJapan;
		}

		#endregion

	}
}